// Contain some general functions used in the app //

function OnlyNumbers(string) {

	var newstring = '';
	for(var i=0;i<string.length;i++) {
		newstring += isNaN(string.substr(i, 1)) ? '' : string.substr(i, 1);
	}
	return newstring;

}

// Function to Get the current file name //
// /!\ Does not work !!! /!\ //
function GetFileName() {
	alert("///////////////// ENTER GetFileName() ///////////////////////");
    var scripts = document.getElementsByTagName('SCRIPT');
    var path = '';
    alert("scripts = "+scripts);
    if(scripts && scripts.length>0) {
    	alert("////////// ENTER IF 1 ////////////////");
        for(var i in scripts) {
        	alert("////////// ENTER FOR i="+i+" ////////////////");
        	alert("script["+i+"] = "+scripts[i]);
        	alert("script["+i+"].scr = " + scripts[i].src);
        	alert("scripts["+i+"].src.match(/script\.js$/) = "+scripts[i].src.match(/script\.js$/));
            if(scripts[i].src && scripts[i].src.match(/script\.js$/)) {
                path = scripts[i].src.replace(/(.*)script\.js$/, '$1');
            }
        }
    }
    alert("path = "+path);
    return path;
}    
 
function include(src, attributes)
{
	try {
		attributes = attributes || {};
		attributes.type = "text/javascript";
		attributes.src = src;

		var script = document.createElement("script");
		for(aName in attributes)
			script[aName] = attributes[aName];

		document.getElementsByTagName("head")[0].appendChild(script);
		return true;
	} catch(e) { return false; }
}

function StartingVideoYoutubeScene(CurrentScene){
	alert('in Function  StartingVideoScene  CurrentScene ='+CurrentScene); 

	switch (CurrentScene){

	case "Scene2":
			alert(' StartingVideoScene  Scene2 '+CurrentScene);	
	    
			NextScene = "Scene2";
			VideoName = "GEdZA-r-Nd0";

			sf.scene.hide(''+CurrentScene+'');
			sf.scene.show('PlayerYoutube');
			sf.scene.focus('PlayerYoutube');

		break;		
	}
	
}

/*function StartingplayerScene(CurrentScene){
	alert('in Function  StartingVideoScene  CurrentScene ='+CurrentScene); 

	switch (CurrentScene){

		case "Scene2":
				alert(' StartingVideoScene  Scene2 '+CurrentScene);	
		    
				NextScene = "EnigmaBalls";
				VideoName = "basket.avi";
				
				setTimeout(function(){
					// On lance la page vidéo
						sf.scene.hide(CurrentScene);
						sf.scene.show('Player');
						sf.scene.focus('Player');
					}, 1300 );
				return 1;
			break;
		default:
			return 0;
			alert("no video");
	}	
}*/

function StartingplayerScene(id){
	alert('in Function  StartingVideoScene  id ='+id);
	alert("currentScene = "+CurrentScene);
	
	if(id != -1){
		
		NextScene = VideoDataBase[id]["nextscene"];
		VideoName = VideoDataBase[id]["url"];
		
		switch (VideoDataBase[id]["type"]){
			case "youtube":
				if(NextScene == CurrentScene){
					NoHide = true;
					alert("******************** NextScene == CurrentScene");
					setTimeout(function(){
						// On lance la page vidéo
							sf.scene.show('PlayerYoutube');
							sf.scene.focus('PlayerYoutube');
						}, 1300 );
					
				}else{
					NoHide = false;
					alert("******************** NextScene != CurrentScene");
					setTimeout(function(){
						// On lance la page vidéo
							sf.scene.hide(''+CurrentScene+'');
							sf.scene.show('PlayerYoutube');
							sf.scene.focus('PlayerYoutube');
						}, 1300 );
					
				}				
				return 1;
				break;
			case "local":
				if(NextScene == CurrentScene){
					NoHide = true;
					alert("******************** NextScene == CurrentScene");
					setTimeout(function(){
						// On lance la page vidéo
							sf.scene.show('Player');
							sf.scene.focus('Player');
						}, 1300 );
				}else {
					NoHide = false;
					alert("******************** NextScene != CurrentScene");
					setTimeout(function(){
						// On lance la page vidéo
							sf.scene.hide(CurrentScene);
							sf.scene.show('Player');
							sf.scene.focus('Player');
						}, 1300 );
				}
				
				return 1;
				break;
			default:
				alert("invalid type value for VideoDataBase id:"+id);
				return 0;
		}
		
	}
	return 0;
}

function Enter(){
	alert('AddButton = '+AddButton+' DisplayPopUp = '+DisplayPopUp+' PlayVideo = '+PlayVideo );
	
	if(AddButton == true && DisplayPopUp == false ){
		PosDialButton = (PosDialButton+1)%NbButton;
		// On réinitialise le nb de bouton aprés validation du joueur
		//On récupére le nom de la scene a charger
		var SceneNumber = $('.Choice'+PosDialButton).attr('value').split(';');
		if(SceneNumber[1] != null || SceneNumber[1] != ""){
		AddPoint(SceneNumber[1]);
		}
		scene = SceneNumber[0]+'.json';
		choice = SceneNumber[0];
		alert(' SCENE NAME ENTER '+scene);
		//on réinitilise le compteur de ligne
		compteur= 0; // Compteur ligne 
		NbButton = 0; // On reinitialise le nbre de boutton
		AddButton = false; 
		$('#container, #container-pnj').children().removeClass("selected");
		
		if(SceneNumber[2] != null && SceneNumber[2] != "" && SceneNumber[2] != "undefined"){
			if( StartingplayerScene(VideoId) == 0){
				alert(' Changing scene'+SceneNumber[2]);
				sf.scene.hide(CurrentScene);
				sf.scene.show(SceneNumber[2]);
				sf.scene.focus(SceneNumber[2]);
				alert("************************************** "+CurrentScene);
			}
		}

	}else {
		HidePopUp();
	}
	if(!PlayVideo){
		OneTime();
	}	
}


// Volume key event
onShowEvent = function() {
    var nnaviPlugin = document.getElementById('pluginObjectNNavi');
    nnaviPlugin.SetBannerState(PL_NNAVI_STATE_BANNER_VOL);
    PL_NNAVI_STATE_BANNER_NONE = 0;
    PL_NNAVI_STATE_BANNER_VOL = 1;
    PL_NNAVI_STATE_BANNER_VOL_CH = 2;

    // For volume OSD
    pluginAPI.unregistKey(tvKey.KEY_VOL_UP);
    pluginAPI.unregistKey(tvKey.KEY_VOL_DOWN);
    pluginAPI.unregistKey(tvKey.KEY_MUTE);
};